<?php

namespace app\admin\controller;

use app\common\business\UserAssociation;
use app\common\business\UserInfo;
use app\common\lib\Param;
use app\common\lib\Show;
use think\response\Json;

class WxUser extends AdminBase
{
    /**
     * 管理后台列表
     * @return Json
     */
    public function list(): Json
    {
        list($name, $page,$limit,$sex,$userType) = Param::getMore([
            ['name', ''],
            ['page', 1],
            ['limit', 15],
            ['sex',''],
            ['userType',0],
        ], $this->request, true);
        try {
            $list=(new \app\common\business\User())->getTableList($page,$limit,$name,$sex,$userType);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        return Show::success($list);
    }
    /**
     * 根据ID 获取用户详情
     * @return Json
     */
    public function detail(): Json
    {
        $id=$this->request->get('id');
        try {
            $result=(new \app\common\business\User())->getDetail($id);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        return Show::success($result);
    }


    /**
     * 根据用户ID获取用户信息
     * @return Json
     */
    public function info(): Json
    {
        $id=$this->request->get('id');
        if(empty($id)){
            return Show::error([],'参数错误');
        }
        try {
            $result=(new UserInfo())->getNormalInfoByField('user_id',$id);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        return Show::success($result);
    }

    /**
     * 根据用户ID 获取关联企业信息
     * @return Json
     */
    public function enterprise(): Json
    {
        $id=$this->request->get('id');
        if(empty($id)){
            return Show::error([],'参数错误');
        }
        try {
            $result=(new UserAssociation())->getNormalByField('user_id',$id);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        return Show::success($result);
    }

    /**
     * 审核企业
     * @return Json
     */
    public function audit(): Json
    {
        list($userId, $audit,$remark) = Param::getMore([
            ['user_id',null],
            ['audit',null],
            ['remark',null]
        ], $this->request, true);
        if(empty($userId)||empty($audit)){
            return Show::error([], '参数错误');
        }
        if(empty($remark)){
            return Show::error([], '审核备注不能为空');
        }
        try {
            $result = (new UserAssociation())->audit($userId,$audit,$remark);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "审核成功");
        }
        return Show::error([], '审核失败');
    }
}
